
import random

#from util import *
from dbfunction import Databasefunction
db = Databasefunction()


class User():
    def zhuce():
        uname = input_uname()
        #当函数的返回值等于uname的时候
        #进入密码验证
        upwd = input_upwd()
        ubalance = random.randrange(100,1000,100)
        #将用户写入用户文件
        sql = 'insert into woniuatm values (0,'uname','upwd','ubalance')'
        db = Databasefunction()
        db.update(sql)
        #userinfo = uname + ',' + upwd + ',' + str(ubalance) + '\n'
        #write_userinfo(userinfo)
        from menu import start_menu
        start_menu()







    def input_uname():
        uname = input('请输入你的用户名')
        #检查用户名是否存在
        if check_uname(uname):
            print('用户名已经存在')
            uname = input_uname()
        return uname
        #递归函数,用户输入用户名时,函数进行判断,当判断为存在是,返回一个下标,
        #代表存在,当判断为假时,将新的名字传递给'uname'

    def input_upwd():
        upwd = input('请输入你的密码')
        return upwd

    def check_uname(uname):
        sql = 'select uname from woniuatm where uname = {}'.format(uname)
        db.select(sql)
        if result != None:
            return uname
        else:
            return False

        '''
        #读取文件
        user_list = read_userinfo()
        for i in range(1,len(user_list)):
            if user_list[i].split(',')[0] == uname:
                return i
            
        return False
        '''


    def denglu():
        uname = input('请输入你的用户名')
        upwd = input('请输入你的密码')
        if check_uname(uname)
            sql = 'select upwd,id from woniuatm where uname = {}'.format(uname)
            db = Databasefunction()
            db.select(sql)
            if result != None and result[0] == upwd:
                print('登录成功')
                global uid
                uid = u_id

        '''
        index = check_uname(uname)#将下标赋值给一个变量
        if index:
            user_list = read_userinfo()#读取用户信息
            if user_list[index].split(",")[1] == upwd:#下标对应的密码等于输入的密码
                print("恭喜登录成功")
                global login_user_index
                #声明一个全局变量,以便ubalance对应调用
                login_user_index = index 
                #保存下标
        '''
                from menu import main_menu
                main_menu()
            else:
                print("密码错误!")
        else:
            print("用户名不存在")
        from menu import start_menu
        start_menu()
    